﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using Session;
using PrimaryKeyFunction;


public partial class Admin_Material : System.Web.UI.Page
{
    Service objService = new Service();
    Functions objFunction = new Functions();


    private void bindDropdownlist()
    {
      
        DataSet ds = new DataSet();
        ds = objService.GetMaterialAllClass();
        ddlClass.DataTextField = ds.Tables[0].Columns[2].ToString();
        ddlClass.DataSource = ds.Tables[0];
        ddlClass.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string Expid = Request.QueryString["experiment_id"];

            if (Expid != null)
            {
                Session["ExperimentId"] = Expid;
                Session["YieldId"] = objFunction.GetExpYieldId(Expid);
                Session["LiquidCompId"] = objFunction.ExpGetLiquidComId(Expid);
            }
           
            string ExperimentId = (string)Session["ExperimentId"];

                if (ExperimentId != null)
                {
                    lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
                }

                bindDropdownlist();
                bindDataGrid();

                string id = Request.QueryString["id"];

                if (id != null)
                {
                    fillMaterialDetails(Convert.ToInt32(id));
                    txtMaterialId.Text = string.Empty;
                    trMaterialDg.Visible = false;
                    trMaterialTextBox.Visible = true;
                }

            }           
        
    }

    protected void btnAddClass_Click(object sender, EventArgs e)
    {
       lblMessage.Text = objService.AddMaterialClass(txtAddClass.Text);
       bindDropdownlist();
       txtAddClass.Text = string.Empty;
    }

    protected void chkAddClass_CheckedChanged(object sender, EventArgs e)
    {
        if (chkAddClass.Checked == true)
        {
            trAddClass.Visible = true;
        }
        else
        {
            trAddClass.Visible = false;
        }
    }

    private void bindDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];
        if (ExpId == null)
        {
            lblMessage.Text = "You haven't entered any data for the Material to this experiment";
            trMaterialDg.Visible = false;
            trMaterialTextBox.Visible = true;
        }
        else
        {
            Int32  MaterialId = objService.GetExperimentFKValues("material_fk", ExpId);
            if (MaterialId.ToString() == null)
            {
                lblMessage.Text = "You haven't entered any data for the Material to this experiment";
                trMaterialDg.Visible = false;
                trMaterialTextBox.Visible = true;
            }
            else
            {
                if (objService.GetMaterial(MaterialId.ToString()).Tables[0].Rows.Count > 0)
                {
                    dgMaterial.DataSource = objService.GetMaterial(MaterialId.ToString());
                    dgMaterial.DataBind();
                    trMaterialDg.Visible = true;
                    trMaterialTextBox.Visible = false;
                }
                else
                {
                    trMaterialDg.Visible = false;
                    trMaterialTextBox.Visible = true;
                }
               
            }
        }
    }

    private void ClearTextBoxes()
    {
        txtMaterialName.Text = string.Empty;
        txtHandlinghistory.Text = string.Empty;
        txtSource.Text = string.Empty;
        txtMaterialId.Text = string.Empty;
        txtAddClass.Text = string.Empty;
    }
        
    private void fillMaterialDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("material", id);
        txtMaterialId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtMaterialName.Text = ds.Tables[0].Rows[0]["name"].ToString();
        ddlClass.Text = ds.Tables[0].Rows[0]["class"].ToString();
        txtSource.Text = ds.Tables[0].Rows[0]["source"].ToString();
        txtHandlinghistory.Text = ds.Tables[0].Rows[0]["handlingHistory"].ToString();           
    }

    protected void dgMaterial_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgMaterial.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillMaterialDetails(id);
        trMaterialDg.Visible = false;
        trMaterialTextBox.Visible = true;
        btnPreviousMaterials.Visible = false;
    }

    protected void btnSave_Click1(object sender, ImageClickEventArgs e)
    {
        string ReferenceID = (string)Session["ReferenceId"];

        if (txtMaterialId.Text != string.Empty)
        {
           
            if (txtMaterialName.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("material", "name", "null" , txtMaterialId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("material", "name", txtMaterialName.Text, txtMaterialId.Text);
            }

            
            lblMessage.Text = objService.UpdateTable("material", "class", ddlClass.SelectedValue.ToString(), txtMaterialId.Text);
            if (txtSource.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("material", "source", "null", txtMaterialId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("material", "source", txtSource.Text, txtMaterialId.Text);
            }

            if (txtHandlinghistory.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("material", "handlingHistory", "null", txtMaterialId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("material", "handlingHistory", txtHandlinghistory.Text, txtMaterialId.Text);
            }

            bindDataGrid();

            ClearTextBoxes();
        }
        else
        {

           // string MaterialIdValue = objService.GetMaterialId(ddlClass.SelectedItem.ToString()).ToString();

            if (ddlClass.Items.Count < 1)
            {
                lblMessage.Text = objService.SaveMaterial(txtMaterialName.Text, "", txtSource.Text, txtHandlinghistory.Text);
            }
            else
            {
                lblMessage.Text = objService.SaveMaterial(txtMaterialName.Text, ddlClass.SelectedValue.ToString(), txtSource.Text, txtHandlinghistory.Text);
            }

            string MaterialId = objService.GetLastInsertedId("ref_fk", ReferenceID, "material");
               
            //objService.GetMaterialRecentId();

            objService.UpdateTable("material", "ref_fk", ReferenceID , MaterialId);

            ClearTextBoxes();        
    
            if ((string)Session["ExperimentId"] != null)
            {
                string ExpId = (string)Session["ExperimentId"];

                if (objService.UpdateExperiment("material_fk", MaterialId, ExpId) != "Experiment Successfully Updated")
                {
                    objService.DeleteRecord("material", Convert.ToInt32(MaterialId));
                    lblMessage.Text = "Admin - You can't enter the multiple Material records for one experiment";
                }
                else
                {
                    lblMessage.Text = "Material Saved and " + objService.UpdateExperiment("material_fk", MaterialId, ExpId);
                }
                lblExpdetails.Text = "Your Experiment Id is - " + ExpId;

                bindDataGrid();
            }
            else
            {

                lblMessage.Text = "Material Saved and " + objService.SaveExperiment(null, null, null, null, MaterialId, null, null, null, null, null, null, ReferenceID,null);
                string ExperimentId = objService.GetExperimentId("material_fk", MaterialId);
                Session["ExperimentId"] = ExperimentId;
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;

                bindDataGrid();
            }
        }
    }

    protected void dgMaterial_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgMaterial.PageIndex = e.NewPageIndex;
        bindDataGrid();
    }

    protected void btnPreviousMaterials_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreMaterial.aspx");
    }
}